package com.jokerhung.mymoney;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class edittrans extends Activity {
	private SQLiteDatabase db=null;
	private Cursor editCursor=null;
	
	private TextView txtFromfor;
	private EditText edtDate;
	private EditText edtFromfor;
	private EditText edtAmount;
	private Spinner spinCategory;
	private Button butSave;
	private Button butCancel;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        
        this.txtFromfor = (TextView) findViewById(R.id.edit_txtFromfor);
        this.edtDate = (EditText) findViewById(R.id.edit_edtDate);
        this.edtFromfor = (EditText) findViewById(R.id.edit_edtFromfor);
        this.edtAmount = (EditText) findViewById(R.id.edit_edtAmount);
        this.spinCategory = (Spinner) findViewById(R.id.edit_spinCategory);
        this.butSave = (Button) findViewById(R.id.edit_butSave);
        this.butCancel = (Button) findViewById(R.id.edit_butCancel);
        
        db = (new DatabaseHelper(this)).getWritableDatabase();
        
        if (getData(Variables.current_transid) > 0) {
        	fillUIwithData();
        }
        
        this.butSave.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		doSave();
            }
        });
        
        this.butCancel.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		doCancel();
            }
        });
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	
    	editCursor.close();
    	db.close();
    }
    
    private int getData(long transid) {
    	String[] transid_string={String.valueOf(transid)};
    	
    	editCursor = db.query("transactions", new String[]{"date","fromfor", "amount", "category_id"}, 
				"_ID=?", transid_string, null, null, null);
    	
    	if (editCursor.getCount() > 0) {
    		
    		editCursor.moveToFirst();
    		
    		Variables.current_date = editCursor.getString(0);
    		Variables.current_fromfor = editCursor.getString(1);
    		Variables.current_amount = editCursor.getLong(2);
    		Variables.current_categoryid = editCursor.getInt(3);
    	}
    	
    	return(editCursor.getCount());
    }
    
    private void fillUIwithData() {
    	this.edtDate.setText(Variables.current_date);
    	this.edtFromfor.setText(Variables.current_fromfor);
    	this.edtAmount.setText(String.valueOf(Variables.current_amount));
    }

    private void doSave() {
    	
    }
    
    private void doCancel() {
    	
    }
}
